package com.cater.task.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.Dictionarydata;
import com.cater.sys.service.impl.DictionarydataServiceImpl;
import com.cater.task.entity.TaskCompletion;
import com.cater.task.entity.TaskManager;
import com.cater.task.entity.TaskSubManager;
import com.cater.task.service.TaskCompletionServiceImpl;
import com.cater.task.service.TaskManagerServiceImpl;
import com.cater.task.service.TaskSubManagerServiceImpl;

/**
 * 描述：任务完成情况控制层
 * 
 * @author yong
 * @date 2018/09/04
 */
@Controller
@RequestMapping("/taskCompletion")
public class TaskCompletionController {

	@Autowired
	private TaskCompletionServiceImpl service;
	@Autowired
	private DictionarydataServiceImpl dictService;// 数据字典
	@Autowired
	private TaskManagerServiceImpl taskManagerService;
	@Autowired
	private TaskSubManagerServiceImpl subManagerService;

	/**
	 * @任务完成情况管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "task/taskCompletion/list";
	}

	/**
	 * @任务完成情况管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询任务完成情况分页数据")
	public PageResult<TaskCompletion> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<TaskCompletion> result = service.findPageResult(query);
		Dictionarydata dict = null;
		for (TaskCompletion taskCompletion : result.getRows()) {
			String type = taskCompletion.getType();
			if (StringUtils.isNotEmpty(type)) {
				dict = new Dictionarydata();
				dict = dictService.getByTypeValue(type, "task_progress");
				if (dict != null) {
					taskCompletion.setDictType(dict.getValueName());
				} else {
					taskCompletion.setDictType("无");
				}
			}
		}
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("taskCompletion:add")
	public String addView(String taskId, String status, Model model) {
		if (StringUtils.isNotEmpty(taskId)) {
			model.addAttribute("taskId", taskId);
		}
		if (StringUtils.isNotEmpty(status)) {
			model.addAttribute("status", status);
		}
		return "task/taskCompletion/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("taskCompletion:add")
	@ResponseBody
	@SystemControllerLog(description = "添加任务完成情况")
	public JSONObject add(TaskCompletion t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String id = service.saves(t);
		String status = t.getStatus();
		String taskId = t.getTaskId();
		TaskManager taskManager = null;
		if (StringUtils.isNotBlank(id)) {
			if (StringUtils.isNotEmpty(status)) {
				taskManager = new TaskManager();
				taskManager = taskManagerService.getById(taskId);
				if (taskManager != null) {
					taskManager.setStatus(Integer.valueOf(status));
					taskManagerService.update(taskManager);
				} else {
					TaskSubManager subManager = subManagerService.getById(taskId);
					if (subManager != null) {
						String mianId = subManager.getTaskId();
						subManager.setStatus(status);
						subManagerService.update(subManager);
						if (StringUtils.isNotEmpty(mianId)) {
							taskManager = new TaskManager();
							taskManager = taskManagerService.getById(mianId);
							if (taskManager != null) {
								/**
								 * 未完成，任务进行中
								 */
								if (taskManager.getStatus() == 0) {
									taskManager.setStatus(1);
								}
							}
							taskManagerService.update(taskManager);
						}
					}
				}
			}
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("taskCompletion:edit")
	public String editView(String id, Model model, CommonQuery query) {
		TaskCompletion t = service.getById(id);
		model.addAttribute("taskCompletion", t);
		return "task/taskCompletion/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("taskCompletion:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改任务完成情况")
	public JSONObject update(TaskCompletion t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("taskCompletion:delete")
	@SystemControllerLog(description = "删除任务完成情况")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("taskCompletion:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		TaskCompletion t = service.getById(id);
		model.addAttribute("taskCompletion", t);
		return "task/taskCompletion/detail";
	}

	/**
	 * 
	 * @Description:根据任务ID查询
	 * @param taskId
	 * @param model
	 * @return String
	 * @time:2019年4月19日 下午9:49:00
	 */
	@RequestMapping("getCompletion")
	public String getCompletion(String taskId, Model model) {
		try {
			if (StringUtils.isNotEmpty(taskId)) {
				List<TaskCompletion> completions = service.getByTaskId(taskId);
				if (completions.size() > 0) {
					model.addAttribute("completions", completions);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "task/taskCompletion/completeInfo";
	}
}